package controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import net.sf.json.JSONObject;
import object.wxtemplate.GetPP2CMsg;
import object.wxtemplate.GetP2UMsg;
import object.wxtemplate.GetedP2UMsg;
import object.wxtemplate.GetedPP2UMsg;
import object.wxtemplate.WxZcMsg;
import object.wxtemplate.WxZcSendMsg;
import object.wxtemplate.XyStatusMsg;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import service.UserService;
import utils.JsonUtil;
import utils.MD5Helper;
import utils.UrlParamUtil;
import utils.ValidateUtil;
import utils.weixin.TemplateData;
import utils.weixin.WeixinUtil;
import utils.weixin.WxTemplate;
import config.ConstantConfig;
import config.SysConfig;

/**
 * 
 * @author hdq
 *
 */
@Controller
@RequestMapping("wxMessage")
public class WxMessageController {

	@Autowired
	private UserService userService;
	
	private static Logger logger = Logger.getLogger(WxMessageController.class);
	
	private final static String USER2COURIER_HANDLE="1" ;//快递员及用户可取情况：
	private final static String COURIER_HANDLE="2" ;//快递员及用户可取情况：
	
	private final static String GET_CLOTHE="1" ;//等待收衣
	private final static String SEND_CLOTHE="4" ;//等待送衣
	private final static String CHECK_ORDER="1" ;//订单审核
	private final static String CANCLE_ORDER="6" ;//订单取消
	
	
	/**
	 * 1001接口
	 * 快递箱取件提醒
	 */
	@RequestMapping("getPP2U")
	@ResponseBody
	public Map<String, Object> getPP2U(
			@RequestParam(value = "sign") String sign,
			@RequestParam(value = "jsonData") String jsonData) {
		HashMap<String, Object> response = new HashMap<>();
		try {
			GetP2UMsg getPP2UMsg=JsonUtil.jsonToBean(jsonData, GetP2UMsg.class);
			String openId=getOpenId(getPP2UMsg.getUserMobileNum());
			if(!ValidateUtil.isValids(openId)){
				response.put("resultCode","40000");
				response.put("resultMsg", "openId is null");
				return response;
			}
			if(checkSign(getPP2UMsg.getParamStr(),SysConfig.agent_key,sign)){
				Map<String,TemplateData> m = new HashMap<String,TemplateData>();
				TemplateData first = new TemplateData(); 
				String firstStr="您的"+getPP2UMsg.getExpCompanyName()+"（"+getPP2UMsg.getParcelNum()+"）包裹"
						+ "已存入"+getPP2UMsg.getDevBranchAddr()
						+"邮宝快递柜"+getPP2UMsg.getBoxNum()+"号箱";
		        first.setColor("#333399");  
		        first.setValue(firstStr);  
		        m.put("first", first);  
		        
		        TemplateData phonenum = new TemplateData();  
		        phonenum.setColor("#000000");  
		        phonenum.setValue(getPP2UMsg.getUserMobileNum());  
		        m.put("phonenum", phonenum); 
		        
		        TemplateData password = new TemplateData();  
		        password.setColor("#ff0000");  
		        password.setValue(getPP2UMsg.getGetCode());  
		        m.put("password", password); 
		        
		        TemplateData savetime = new TemplateData();  
		        savetime.setColor("#000000");  
		        //savetime.setValue(getPP2UMsg.getStoreTime()); 
		        savetime.setValue(getFormatStr(getPP2UMsg.getStoreTime()));
		        m.put("savetime", savetime); 
		        
		        TemplateData remark = new TemplateData();  
		        remark.setColor("#000000");  
		        String remarkStr="请及时取走，超期将被快递员取回。有任何问题请联系客服热线"+getPP2UMsg.getDevBranchHotLine()+"。";
		        remark.setValue(remarkStr);  
		        m.put("remark", remark); 
				
				WxTemplate t = new WxTemplate(); 
				t.setUrl(SysConfig.openLockUI_url+"?openId=" +UrlParamUtil.changURl(openId)+ "&jsonData="+jsonData);
				//t.setUrl("http://192.168.1.148:8080/YouBoxService/web/openLock.jsp");
				//t.setUrl("");
				t.setTemplate_id(SysConfig.get_p_2umsgid);
				t.setTopcolor("#000000");
				t.setTouser(openId);
		        t.setData(m);
		        
		        String postData=WeixinUtil.getPostJsonData(t);
				String url=SysConfig.mssage_template_url+WeixinUtil.getAccessToken(SysConfig.appid, SysConfig.appSecret).getToken();
				JSONObject jsonObject=WeixinUtil.httpRequest(url, "POST", postData);
				
				response.put("resultCode",jsonObject.getString("errcode"));
				response.put("resultMsg", jsonObject.getString("errmsg"));
			}else{
				response.put("resultCode","40001");
				response.put("resultMsg", "sign error");
			}
		} catch (Exception e) {
			logger.error(e.getMessage());
			response.put("resultCode","-1");
			response.put("resultMsg", "request failed");
		}
		return response;
	}
	
	/**
	 * 1002接口
	 * 用户取件成功通知
	 */
	@RequestMapping("getedP2U")
	@ResponseBody
	public Map<String, Object> getedP2U(
			@RequestParam(value = "sign") String sign,
			@RequestParam(value = "jsonData") String jsonData) {
		HashMap<String, Object> response = new HashMap<>();
		try {
			GetedP2UMsg getedP2UMsg=JsonUtil.jsonToBean(jsonData, GetedP2UMsg.class);
			String openId=getOpenId(getedP2UMsg.getUserMobileNum());
			if(!ValidateUtil.isValids(openId)){
				response.put("resultCode","40000");
				response.put("resultMsg", "openId is null");
				return response;
			}
			if(checkSign(getedP2UMsg.getParamStr(),SysConfig.agent_key,sign)){
				Map<String,TemplateData> m = new HashMap<String,TemplateData>();
				TemplateData first = new TemplateData(); 
				String firstStr="您在"+getedP2UMsg.getDevBranchShortAddr()+getedP2UMsg.getBoxNum()+"号箱内的包裹（"+getedP2UMsg.getParcelNum()+"）已被取走。";
		        first.setColor("#333399");  
		        first.setValue(firstStr);  
		        m.put("first", first);  
		        
		        TemplateData keyword1 = new TemplateData();  
		        keyword1.setColor("#000000");  
		        keyword1.setValue(getedP2UMsg.getUserMobileNum());  
		        m.put("keyword1", keyword1); 
		        
		        TemplateData keyword2 = new TemplateData();  
		        keyword2.setColor("#000000");  
		        //keyword2.setValue(getedP2UMsg.getGetTime());  
		        keyword2.setValue(getFormatStr(getedP2UMsg.getGetTime()));
		        m.put("keyword2", keyword2); 
		        
		        TemplateData remark = new TemplateData();  
		        remark.setColor("#000000");  
		        String remarkStr="如有疑问请拨打客服热线:"+getedP2UMsg.getDevBranchHotLine()+"。";
		        remark.setValue(remarkStr);  
		        m.put("remark", remark); 
				
				WxTemplate t = new WxTemplate(); 
				t.setUrl("");
				t.setTemplate_id(SysConfig.geted_p_2umsgid);
				t.setTopcolor("#000000");
				t.setTouser(openId);
		        t.setData(m);
		        
		        String postData=WeixinUtil.getPostJsonData(t);
				String url=SysConfig.mssage_template_url+WeixinUtil.getAccessToken(SysConfig.appid, SysConfig.appSecret).getToken();
				JSONObject jsonObject=WeixinUtil.httpRequest(url, "POST", postData);
				
				response.put("resultCode",jsonObject.getString("errcode"));
				response.put("resultMsg", jsonObject.getString("errmsg"));
			}else{
				response.put("resultCode","40001");
				response.put("resultMsg", "sign error");
			}
		} catch (Exception e) {
			logger.error(e.getMessage());
			e.printStackTrace();
			response.put("resultCode","-1");
			response.put("resultMsg", "request failed");
		}
		return response;
	}
	
	/**
	 * 1003接口
	 * 通知用户取问题件被取走
	 */
	@RequestMapping("getedPP2U")
	@ResponseBody
	public Map<String, Object> getedPP2U(
			@RequestParam(value = "sign") String sign,
			@RequestParam(value = "jsonData") String jsonData) {
		HashMap<String, Object> response = new HashMap<>();
		try {
			GetedPP2UMsg getedPP2UMsg=JsonUtil.jsonToBean(jsonData, GetedPP2UMsg.class);
			String openId=getOpenId(getedPP2UMsg.getUserMobileNum());
			if(!ValidateUtil.isValids(openId)){
				response.put("resultCode","40000");
				response.put("resultMsg", "openId is null");
				return response;
			}
			if(checkSign(getedPP2UMsg.getParamStr(),SysConfig.agent_key,sign)){
				Map<String,TemplateData> m = new HashMap<String,TemplateData>();
				TemplateData first = new TemplateData(); 
				String firstStr="您存放于"+getedPP2UMsg.getDevBranchShortAddr()+getedPP2UMsg.getBoxNum()+"号箱内的包裹";
		        first.setColor("#000000");  
		        first.setValue(firstStr);  
		        m.put("first", first);  
		        
		        TemplateData keyword1 = new TemplateData();  
		        keyword1.setColor("#ff0000");  
		        keyword1.setValue(getedPP2UMsg.getParcelNum());  
		        m.put("keyword1", keyword1); 
		        
		        TemplateData keyword2 = new TemplateData();  
		        keyword2.setColor("#ff0000");  
		        keyword2.setValue(getedPP2UMsg.getGetCode());  
		        m.put("keyword2", keyword2); 
		        
		        TemplateData remark = new TemplateData();  
		        remark.setColor("#333399");  
		        String remarkStr="因为"+getedPP2UMsg.getParcelType()+"被快递员取走，以上信息已失效，请知悉。有任何问题请与快递员"+getedPP2UMsg.getCourierMobileNum()+"联系。";
		        remark.setValue(remarkStr);  
		        m.put("remark", remark); 
				
				WxTemplate t = new WxTemplate(); 
				t.setUrl("");
				t.setTemplate_id(SysConfig.geted_pp_2umsgid);
				t.setTopcolor("#000000");
				t.setTouser(openId);
		        t.setData(m);
		        
		        String postData=WeixinUtil.getPostJsonData(t);
				String url=SysConfig.mssage_template_url+WeixinUtil.getAccessToken(SysConfig.appid, SysConfig.appSecret).getToken();
				JSONObject jsonObject=WeixinUtil.httpRequest(url, "POST", postData);
				
				response.put("resultCode",jsonObject.getString("errcode"));
				response.put("resultMsg", jsonObject.getString("errmsg"));
			}else{
				response.put("resultCode","40001");
				response.put("resultMsg", "sign error");
			}
		} catch (Exception e) {
			logger.error(e.getMessage());
			response.put("resultCode","-1");
			response.put("resultMsg", "request failed");
		}
		return response;
	}
	
	/**
	 * 1004接口
	 * 通知快递员取问题件
	 */
	@RequestMapping("getPP2C")
	@ResponseBody
	public Map<String, Object> getPP2C(
			@RequestParam(value = "sign") String sign,
			@RequestParam(value = "jsonData") String jsonData) {
		HashMap<String, Object> response = new HashMap<>();
		try {
			GetPP2CMsg getPP2CMsg=JsonUtil.jsonToBean(jsonData, GetPP2CMsg.class);
			String openId=getOpenId(getPP2CMsg.getCourierMobileNum());
			
			if(checkSign(getPP2CMsg.getParamStr(),SysConfig.agent_key,sign)){
				Map<String,TemplateData> m = new HashMap<String,TemplateData>();
				TemplateData first = new TemplateData(); 
				String firstStr="您存放于"+getPP2CMsg.getDevBranchShortAddr()+getPP2CMsg.getBoxNum()+"号箱内的包裹";
		        first.setColor("#000000");  
		        first.setValue(firstStr);  
		        m.put("first", first);  
		        
		        TemplateData keyword1 = new TemplateData();  
		        keyword1.setColor("#333399");  
		        keyword1.setValue(getPP2CMsg.getParcelNum());  
		        m.put("keyword1", keyword1); 
		        
		        TemplateData keyword2 = new TemplateData();  
		        keyword2.setColor("#000000");  
//		        keyword2.setValue(getPP2CMsg.getStoreTime());  
		        keyword2.setValue(getFormatStr(getPP2CMsg.getStoreTime()));  
		        m.put("keyword2", keyword2); 
		        
		        TemplateData keyword3 = new TemplateData();  
		        keyword3.setColor("#000000");  
		        keyword3.setValue(getPP2CMsg.getUserMobileNum());  
		        m.put("keyword3", keyword3); 
		        
		     // 仅快递员可取情况：
		        String remarkStr="";
		        if(USER2COURIER_HANDLE.equals(getPP2CMsg.getHandleType())){
		        	getPP2U(getPP2CMsg,sign);
		        	remarkStr="已被设置成"+getPP2CMsg.getParcelType()+"，请您及时回收。有任何问题，请联系客服热线"+getPP2CMsg.getDevBranchHotLine()+"。";
		        }else if(COURIER_HANDLE.equals(getPP2CMsg.getHandleType())){
		        	remarkStr="已被设置成"+getPP2CMsg.getParcelType()+"，请您及时回收。有任何问题，请联系客服热线"+getPP2CMsg.getDevBranchHotLine()+"。";
		        }
		        if(!ValidateUtil.isValids(openId)){
					response.put("resultCode","40000");
					response.put("resultMsg", "openId is null");
					return response;
				}
		        TemplateData remark = new TemplateData();  
		        remark.setColor("#ff0000"); 
		        remark.setValue(remarkStr);  
		        m.put("remark", remark); 
				
				WxTemplate t = new WxTemplate(); 
				t.setUrl("");
				t.setTemplate_id(SysConfig.get_pp_2cmsgid);
				t.setTopcolor("#000000");
				t.setTouser(openId);
		        t.setData(m);
		        
		        String postData=WeixinUtil.getPostJsonData(t);
				String url=SysConfig.mssage_template_url+WeixinUtil.getAccessToken(SysConfig.appid, SysConfig.appSecret).getToken();
				JSONObject jsonObject=WeixinUtil.httpRequest(url, "POST", postData);
				
				response.put("resultCode",jsonObject.getString("errcode"));
				response.put("resultMsg", jsonObject.getString("errmsg"));
			}else{
				response.put("resultCode","40001");
				response.put("resultMsg", "sign error");
			}
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.getMessage());
			response.put("resultCode","-1");
			response.put("resultMsg", "request failed");
		}
		return response;
	}
	
	
	public Map<String, Object> getPP2U(GetPP2CMsg getPP2CMsg,String sign){
		HashMap<String, Object> response = new HashMap<>();
		try {
			String openId=getOpenId(getPP2CMsg.getUserMobileNum());
			if(!ValidateUtil.isValids(openId)){
				response.put("resultCode","40000");
				response.put("resultMsg", "openId is null");
				return response;
			}
			if(checkSign(getPP2CMsg.getParamStr(),SysConfig.agent_key,sign)){
				Map<String,TemplateData> m = new HashMap<String,TemplateData>();
				TemplateData first = new TemplateData(); 
				String firstStr="您存放于"+getPP2CMsg.getDevBranchShortAddr()+getPP2CMsg.getBoxNum()+"号箱内的包裹";
		        first.setColor("#000000");  
		        first.setValue(firstStr);  
		        m.put("first", first);  
		        
		        TemplateData keyword1 = new TemplateData();  
		        keyword1.setColor("#333399");  
		        keyword1.setValue(getPP2CMsg.getParcelNum());  
		        m.put("keyword1", keyword1); 
		        
		        TemplateData keyword2 = new TemplateData();  
		        keyword2.setColor("#000000");  
//		        keyword2.setValue(getPP2CMsg.getStoreTime());  
		        keyword2.setValue(getFormatStr(getPP2CMsg.getStoreTime()));  
		        m.put("keyword2", keyword2); 
		        
		        TemplateData keyword3 = new TemplateData();  
		        keyword3.setColor("#000000");  
		        keyword3.setValue(getPP2CMsg.getUserMobileNum());  
		        m.put("keyword3", keyword3); 
		        
		     // 仅快递员可取情况：
		        String remarkStr="已被设置成"+getPP2CMsg.getParcelType()+"，您可联系收件人取走或自己回收该包裹。有任何问题，请联系客服热线"+getPP2CMsg.getDevBranchHotLine()+"。";
		       
		        TemplateData remark = new TemplateData();  
		        remark.setColor("#ff0000"); 
		        remark.setValue(remarkStr);  
		        m.put("remark", remark); 
				
				WxTemplate t = new WxTemplate(); 
				t.setUrl("");
				t.setTemplate_id(SysConfig.get_pp_2cmsgid);
				t.setTopcolor("#000000");
				t.setTouser(openId);
		        t.setData(m);
		        
		        String postData=WeixinUtil.getPostJsonData(t);
				String url=SysConfig.mssage_template_url+WeixinUtil.getAccessToken(SysConfig.appid, SysConfig.appSecret).getToken();
				JSONObject jsonObject=WeixinUtil.httpRequest(url, "POST", postData);
				
				response.put("resultCode",jsonObject.getString("errcode"));
				response.put("resultMsg", jsonObject.getString("errmsg"));
			}else{
				response.put("resultCode","40001");
				response.put("resultMsg", "sign error");
			}
		} catch (Exception e) {
			logger.error(e.getMessage());
			response.put("resultCode","-1");
			response.put("resultMsg", "request failed");
		}
		return response;
	}
	
	/**
	 * 1005接口
	 * 洗衣状态改变通知
	 */
	@RequestMapping("xyStatus")
	@ResponseBody
	public Map<String, Object> xyStatus(
			@RequestParam(value = "sign") String sign,
			@RequestParam(value = "jsonData") String jsonData) {
		HashMap<String, Object> response = new HashMap<>();
		try {
			
			XyStatusMsg statusMsg=JsonUtil.jsonToBean(jsonData, XyStatusMsg.class);
			String openId=getOpenId(statusMsg.getUserMobileNum());
			if(!ValidateUtil.isValids(openId)){
				response.put("resultCode","40000");
				response.put("resultMsg", "openId is null");
				return response;
			}
			if(checkSign(statusMsg.getParamStr(),SysConfig.agent_key,sign)){
				Map<String,TemplateData> m = new HashMap<String,TemplateData>();
				String orderStaus=statusMsg.getOrderStatus();
				String checkStaus=statusMsg.getCheckStatus();
				String firstStr="";
				String remarkStr="";
				String statuStr="";
				if(GET_CLOTHE.equals(orderStaus)){
					firstStr="亲，为您安排好人员收衣,\r\n请您耐心等待，感谢您的支持.";
					statuStr="等待收衣";
					remarkStr="预约收衣时间:"+getFormatStr(statusMsg.getTime())+"\r\n"+
							"服务人员姓名:"+statusMsg.getCourierName()+"\r\n"+
							"服务人员号码:"+statusMsg.getCourierMobileNum()+"\r\n"+
							"\r\n如有任何疑问,请拨打服务热线:"+statusMsg.getHotLine()+".\r\n"+
							"催单或改时间请直接与工作人员联系.";
				}else if(SEND_CLOTHE.equals(orderStaus)){
					String payStatus=statusMsg.getPayStatus();
					String payStr="未支付，请及时支付";
					firstStr="亲,您的衣服已经清洗完成,正火速送到您的手上,\r\n请耐心等待,感谢您的支持.";
					if(ConstantConfig.TRANSACTION_PAYED.equals(payStatus)){
						payStr="已支付";
					}
					statuStr="清洗完成,正在送回";
					remarkStr="支付状态:"+payStr+"\r\n"+
							"预约送衣时间:"+getFormatStr(statusMsg.getTime())+"\r\n"+
							"服务人员姓名:"+statusMsg.getCourierName()+"\r\n"+
							"服务人员号码:"+statusMsg.getCourierMobileNum()+"\r\n"+
							"\r\n如有任何疑问,请拨打服务热线:"+statusMsg.getHotLine()+".\r\n"+
							"催单或改时间请直接与工作人员联系.";
				}else if(CANCLE_ORDER.equals(orderStaus)){
					firstStr="亲,您的订单已被取消,对此给您造成的困惑表示歉意.";
					statuStr="订单取消.";
					remarkStr="\r\n如有任何疑问,请拨打服务热线:"+statusMsg.getHotLine();
				}
				if(CHECK_ORDER.equals(checkStaus)){
					firstStr="亲,您的订单已审核成功可进入支付环节啦~";
					statuStr="审核成功,等待支付.";
					remarkStr="\r\n如有任何疑问,请拨打服务热线:"+statusMsg.getHotLine();
				}
				TemplateData first = new TemplateData(); 
				
		        first.setColor("#000000");  
		        first.setValue(firstStr);  
		        m.put("first", first);  
		        
		        TemplateData OrderSn = new TemplateData();  
		        OrderSn.setColor("#333399");  
		        OrderSn.setValue(statusMsg.getOrderNum());  
		        m.put("OrderSn", OrderSn); 
		        
		        TemplateData OrderStatus = new TemplateData();  
		        OrderStatus.setColor("#ff0000");  
//		        keyword2.setValue(getPP2CMsg.getStoreTime());  
		        OrderStatus.setValue(statuStr);  
		        m.put("OrderStatus", OrderStatus); 
		        
		        TemplateData remark = new TemplateData();  
		        remark.setColor("#000000"); 
		        remark.setValue(remarkStr);  
		        m.put("remark", remark); 
				
				WxTemplate t = new WxTemplate(); 
				t.setUrl("");
				t.setTemplate_id(SysConfig.xy_statausmsgid);
				t.setTopcolor("#000000");
				t.setTouser(openId);
		        t.setData(m);
		        
		        String postData=WeixinUtil.getPostJsonData(t);
				String url=SysConfig.mssage_template_url+WeixinUtil.getAccessToken(SysConfig.appid, SysConfig.appSecret).getToken();
				JSONObject jsonObject=WeixinUtil.httpRequest(url, "POST", postData);
				
				response.put("resultCode",jsonObject.getString("errcode"));
				response.put("resultMsg", jsonObject.getString("errmsg"));
			}else{
				response.put("resultCode","40001");
				response.put("resultMsg", "sign error");
			}
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.getMessage());
			response.put("resultCode","-1");
			response.put("resultMsg", "request failed");
		}
		return response;
	}
	/**
	 * 1006接口
	 * 早餐通知
	 */
	@RequestMapping("getZC")
	@ResponseBody
	public Map<String, Object> getZC(
			@RequestParam(value = "sign") String sign,
			@RequestParam(value = "jsonData") String jsonData) {
		HashMap<String, Object> response = new HashMap<>();
		try {
			
			WxZcMsg zcMsg=JsonUtil.jsonToBean(jsonData, WxZcMsg.class);
			String openId=zcMsg.getOpenId();
			if(!ValidateUtil.isValids(openId)){
				response.put("resultCode","40000");
				response.put("resultMsg", "openId is null");
				return response;
			}
			if(checkSign(zcMsg.getParamStr(),SysConfig.agent_key,sign)){
				Map<String,TemplateData> m = new HashMap<String,TemplateData>();
				TemplateData first = new TemplateData(); 
				String firstStr="您预定的早餐已存放于"+zcMsg.getParcelAddr()+"箱内";
		        first.setColor("#000000");  
		        first.setValue(firstStr);  
		        m.put("first", first); 
		        
		        TemplateData OrderSn = new TemplateData();  
		        OrderSn.setColor("#333399");  
		        OrderSn.setValue(zcMsg.getTid());  
		        m.put("OrderSn", OrderSn); 
		        
		        TemplateData OrderStatus = new TemplateData();  
		        OrderStatus.setColor("#ff0000");  
//		        keyword2.setValue(getPP2CMsg.getStoreTime());  
		        OrderStatus.setValue("已发货");  
		        m.put("OrderStatus", OrderStatus); 
		        
		     
		        String remarkStr="取物码:"+zcMsg.getTakeCode()+"\r\n"+
						"\r\n如有任何疑问,请拨打服务热线:"+zcMsg.getHotLine()+".\r\n";
		        
		        TemplateData remark = new TemplateData();  
		        remark.setColor("#333399");  
		        remark.setValue(remarkStr);  
		        m.put("remark", remark); 
				
				WxTemplate t = new WxTemplate(); 
				t.setUrl("");
				t.setTemplate_id(SysConfig.get_zc_2umsgid);
				t.setTopcolor("#000000");
				t.setTouser(openId);
		        t.setData(m);
		        
		        String postData=WeixinUtil.getPostJsonData(t);
				String url=SysConfig.mssage_template_url+WeixinUtil.getAccessToken(SysConfig.appid_zc, SysConfig.appSecret_zc).getToken();
				JSONObject jsonObject=WeixinUtil.httpRequest(url, "POST", postData);
				
				response.put("resultCode",jsonObject.getString("errcode"));
				response.put("resultMsg", jsonObject.getString("errmsg"));
			}else{
				response.put("resultCode","40001");
				response.put("resultMsg", "sign error");
			}
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.getMessage());
			response.put("resultCode","-1");
			response.put("resultMsg", "request failed");
		}
		return response;
	}
	
	/**
	 * 1007接口
	 * 早餐配送通知
	 */
	@RequestMapping("zcSend")
	@ResponseBody
	public Map<String, Object> zcSend(
			@RequestParam(value = "sign") String sign,
			@RequestParam(value = "jsonData") String jsonData) {
		HashMap<String, Object> response = new HashMap<>();
	try {
			//System.err.println(jsonData);
			WxZcSendMsg sendMsg=JsonUtil.jsonToBean(jsonData, WxZcSendMsg.class);
			String openId=getZcOpenId(sendMsg.getUserId());
			if(!ValidateUtil.isValids(openId)){
				response.put("resultCode","40000");
				response.put("resultMsg", "openId is null");
				return response;
			}
			if(checkSign(sendMsg.getParamStr(),SysConfig.agent_key,sign)){
				Map<String,TemplateData> m = new HashMap<String,TemplateData>();
				String payStatus=sendMsg.getPayStatus();
				String sendTime=sendMsg.getSendTime();
				String firstStr="";
				String remarkStr="";
				String statuStr="";
				firstStr="亲，您"+sendTime+"的早餐已经安排配送,\r\n请您耐心等待，感谢您的支持.";
				statuStr="已支付";
				if("0".equals(payStatus)){
					statuStr="待支付";
				}
				remarkStr="预计送到时间:08:00-10:00\r\n"+
						"服务人员姓名:"+sendMsg.getCourierName()+"\r\n"+
						"服务人员号码:"+sendMsg.getCourierMobileNum()+"\r\n"+
						"\r\n如有任何疑问,请拨打服务热线:"+sendMsg.getHotLine()+".\r\n"+
						"催单请直接与工作人员联系.";
				
				TemplateData first = new TemplateData(); 
				
		        first.setColor("#000000");  
		        first.setValue(firstStr);  
		        m.put("first", first);  
		        
		        TemplateData OrderSn = new TemplateData();  
		        OrderSn.setColor("#333399");  
		        OrderSn.setValue(sendMsg.getOrderNum());  
		        m.put("OrderSn", OrderSn); 
		        
		        TemplateData OrderStatus = new TemplateData();  
		        OrderStatus.setColor("#ff0000");  
//		        keyword2.setValue(getPP2CMsg.getStoreTime());  
		        OrderStatus.setValue(statuStr);  
		        m.put("OrderStatus", OrderStatus); 
		        
		        TemplateData remark = new TemplateData();  
		        remark.setColor("#000000"); 
		        remark.setValue(remarkStr);  
		        m.put("remark", remark);
		        
//		        var url="transaction2zcRouter";
//		    	//var params="funcID=0017&out_trade_no=${param.orderNum}&body=JRZC&total_fee=${param.price}&curPage=${param.curPage}&openid="+$.session.get('platformId');
//		    	var params="funcID=0017&out_trade_no="+orderNum+"&body=JRZC&total_fee="+total+"&curPage="+curP+"&userMp="+userMp;
		    	
				
				WxTemplate t = new WxTemplate(); 
				t.setUrl("");
				t.setTemplate_id(SysConfig.xy_statausmsgid);
				t.setTopcolor("#000000");
				t.setTouser(openId);
		        t.setData(m);
		        
		        String postData=WeixinUtil.getPostJsonData(t);
				String url=SysConfig.mssage_template_url+WeixinUtil.getAccessToken(SysConfig.appid, SysConfig.appSecret).getToken();
				JSONObject jsonObject=WeixinUtil.httpRequest(url, "POST", postData);
				
				response.put("resultCode",jsonObject.getString("errcode"));
				response.put("resultMsg", jsonObject.getString("errmsg"));
			}else{
				response.put("resultCode","40001");
				response.put("resultMsg", "sign error");
			}
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.getMessage());
			response.put("resultCode","-1");
			response.put("resultMsg", "request failed");
		}
		return response;
	}

	
	/**
	 * 暂时的屏蔽接口
	 */
	@RequestMapping("myError")
	@ResponseBody
	public Map<String, Object> myError(
			@RequestParam(value = "sign") String sign,
			@RequestParam(value = "jsonData") String jsonData) {
		HashMap<String, Object> response = new HashMap<>();
		response.put("resultCode","-1");
		response.put("resultMsg", "request failed");
		return response;
	}
	
	
	private String getFormatStr(Long time){
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date date=new Date(time);
		return format.format(date);
	}
	
	private String getOpenId(String userMobileNum){
		return userService.getOpenIdByUserMobileNum(userMobileNum);
		//return "oNtWKjr4azG8R5A7J2gRI4OawFyQ";
	}
	
	private String getZcOpenId(String userId){
		return userService.getZcOpenIdByUserId(userId);
		//return "oNtWKjr4azG8R5A7J2gRI4OawFyQ";
	}
	
	private boolean checkSign(String paramStr,String key,String sign){
		String checkParamStr=paramStr+"&key="+key;
		String iSign=MD5Helper.getMD5Str(checkParamStr);
		sign.equals(iSign);
		return sign.equals(iSign);
	};
}